﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Refl.Extensions
{
    /// <summary>
    /// Static class with extensions to the 'string' type.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Generates a Base64-encoded string.
        /// </summary>
        /// <returns>The Base64 representation of the current string.</returns>
        public static string EncodeToBase64(this string operand)
        {
            byte[] toEncodeAsBytes = System.Text.UTF8Encoding.UTF8.GetBytes(operand);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }
        /// <summary>
        /// Returns a decoded string based on a Base64-encoded string.
        /// </summary>
        /// <returns>The string representation of the current Base64 string.</returns>
        public static string DecodeFromBase64(this string operand)
        {
            byte[] encodedDataAsBytes = System.Convert.FromBase64String(operand);
            string returnValue = System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes);
            return returnValue;
        }
        /// <summary>
        /// Generates the MD5 hash of the current UTF-8 string.
        /// </summary>
        /// <returns>The MD5 hash.</returns>
        public static string GetMD5Hash(this string operand) // Standard
        {
            UTF8Encoding encoder = new UTF8Encoding();
            MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
            byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(operand));
            return hashedDataBytes.ToX2String();
        }
        /// <summary>
        /// Generates the SHA-512 hash of the current UTF-8 string.
        /// </summary>
        /// <returns>The SHA-512 hash.</returns>
        public static string GetSHA512Hash(this string operand) //ULTRA-SAFE
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA512Managed sha512hasher = new SHA512Managed();
            byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(operand));
            return hashedDataBytes.ToX2String();
        }
        /// <summary>
        /// Generates the SHA-1 hash of the current UTF-8 string.
        /// </summary>
        /// <returns>The SHA-1 hash.</returns>
        public static string GetSHA1Hash(this string operand)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA1Managed sha1hasher = new SHA1Managed();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(operand));
            return hashedDataBytes.ToX2String();
        }

    }
    /// <summary>
    /// Static class with extensions to the 'byte' type.
    /// </summary>
    public static class ByteExtensions
    {
        /// <summary>
        /// Convert the byte array to a UTF8 string.
        /// </summary>
        /// <returns>The UTF8 string representation of the byte array.</returns>
        public static string ToUTF8String(this byte[] inputArray)
        {
            return System.Text.UTF8Encoding.UTF8.GetString(inputArray);
        }
        /// <summary>
        /// Convert the byte array to a UTF16 (Unicode) string.
        /// </summary>
        /// <returns>The UTF16 (Unicode) string representation of the byte array.</returns>
        public static string ToUTF16String(this byte[] inputArray)
        {
            return System.Text.UnicodeEncoding.Unicode.GetString(inputArray);
        }
        /// <summary>
        /// Convert the byte array to a UTF32 string.
        /// </summary>
        /// <returns>The UTF32 string representation of the byte array.</returns>
        public static string ToUTF32String(this byte[] inputArray)
        {
            return System.Text.UTF32Encoding.UTF32.GetString(inputArray);
        }
        /// <summary>
        /// Convert the byte array to a ASCII string.
        /// </summary>
        /// <returns>The ASCII string representation of the byte array.</returns>
        public static string ToASCIIString(this byte[] inputArray)
        {
            return System.Text.ASCIIEncoding.ASCII.GetString(inputArray);
        }
        /// <summary>
        /// Convert the byte array to a UTF7 string.
        /// </summary>
        /// <returns>The UTF7 string representation of the byte array.</returns>
        public static string ToUTF7String(this byte[] inputArray)
        {
            return System.Text.UTF7Encoding.UTF7.GetString(inputArray);
        }
        /// <summary>
        /// Convert the byte array to a string using HEX format.
        /// </summary>
        /// <returns>The HEX string representation of the byte array.</returns>
        public static string ToX2String(this byte[] inputArray)
        {
            StringBuilder output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }
    }
}
